package com.games.fifteenpieces;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class SaveLoadActivity extends ListActivity {

	private Saver mSaver;
	private SimpleCursorAdapter mAdapter;
	private Cursor mGamesCursor;
	private Bundle mDump;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Object h = new Object();
		int o = h.hashCode();
		setContentView(R.layout.save_load_layout);
		//this will create/open database
		try{
		mSaver = new Saver(this);
		mSaver.open();
		//fill view with data from db
		mGamesCursor = mSaver.fetchAllGames();
		startManagingCursor(mGamesCursor);
		  //fields in db table
		String[] dbFields = new String[] {Saver.KEY_GAMES_DATE, Saver.KEY_GAMES_COMPLETE};
		  //widgets from layout where will be displayed data from db
		int[] widgets = new int[] {R.id.text_date, R.id.text_complete};
		  //bind db to widgets with adapter
			mAdapter = new SimpleCursorAdapter(this, R.layout.list_item, mGamesCursor, dbFields, widgets);
		
		  //set adapter
		setListAdapter(mAdapter);

		
		//Lets see what way we`ve come here
		Intent i = getIntent();
		mDump = null;
		ListView lv = getListView();
		if (i != null) mDump = i.getExtras();
		if(mDump == null || mDump.getBoolean(MenuActivity.KEY_FROM_MENU)){
			//we are here from menu and want to load a game
			OnItemClickListener itemClickLoadlistener = new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
				    int position, long id){
						load(id);
					}
				};
			lv.setOnItemClickListener(itemClickLoadlistener);	
		} else {
			//we are here from game and want to save a game
			OnItemClickListener itemClickSaveListener = new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int position, long id) {
					save(id);
					
					//mAdapter.notifyDataSetChanged();
				}
			};
			lv.setOnItemClickListener(itemClickSaveListener);
		}
		
		//Register listener for saving new game
		Button btn = (Button) findViewById(R.id.btnSaveNew);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				save(-1);
			}
		});
				
	} catch(Exception e) {
		mSaver.close();
		Log.e("-= SaveLoadActivity =-", "onCreate()", e);
		finish();
	}	
		
	};
	
	protected void load(long id) {
		try {
			mDump = mSaver.load(id);
		} catch (Exception e) {
			Log.e("-= SaveLoad =-", "load()", e);
		}
		mSaver.close();
		Intent i = new Intent();
		i.putExtras(mDump);
		setResult(Activity.RESULT_OK, i);
		finish();
	}

	private void save(long id) {
		try{
			mDump.putLong(Saver.KEY_GAMES_ID, id);
			mSaver.save(mDump);
		} catch (Exception e) {
			Log.e("-= SaveLoad =-", "save()", e);
		}
		mSaver.close();
		setResult(Activity.RESULT_OK);
		finish();

	}
	
	
}
